package com.kly.module_base.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.kly.module_base.view.CustomDialog

abstract class BaseFragment : Fragment() {
    var progressDialog: CustomDialog? = null
    fun showProgressDialog(context: Context?) {
        showProgressDialog(context, false)
    }

    fun showProgressDialog(context: Context?, cancelable: Boolean) {
        showProgressDialog(context, "请稍候", cancelable)
    }

    @SuppressLint("NewApi")
    fun showProgressDialog(context: Context?, text: String?, cancelable: Boolean = false) {
        if (context != null && !(context as Activity).isDestroyed && progressDialog == null) {
            progressDialog = CustomDialog(context, text, cancelable)
            progressDialog!!.show()
        }
    }

    @SuppressLint("NewApi")
    fun hideProgressDialog() {
        if (activity?.isDestroyed != null) {
            progressDialog?.dismiss()
            progressDialog = null
        }
    }

    override fun onDetach() {
        super.onDetach()
        progressDialog?.hide()
    }
}